﻿using Blog.Core.Domain.Model;
using Blog.Core.DTO.ViewModel;
using Blog.Core.IRepositories;
using Blog.Core.IServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Blog.Core.Services
{
    public class logRecordService : IlogRecordIService
    {
        private readonly IRepository<Journal> ilogRecord;
        private readonly IUnOfWork unOfWork;
        public logRecordService(IRepository<Journal> ilogRecord, IUnOfWork unOfWork)
        {
            this.ilogRecord = ilogRecord;
            this.unOfWork = unOfWork;
        }
        /// <summary>
        /// 系统日志的显示（查询）操作
        /// </summary>
        /// <returns></returns>
        public async Task<PageDTO<Journal>> GetLogList(PagelogRecordDTO dto)
        {
            var loge = await ilogRecord.GetAll();
            var list = (from a in loge
                       
                       orderby a.Createtimes descending
                       select a).AsQueryable();
            if (!string.IsNullOrEmpty(dto.Operatorname))
            {
                list = list.Where(x => x.Operatorname.Contains(dto.Operatorname));
            }
            PageDTO<Journal> jou = new PageDTO<Journal>
            {
                RecordCount = list.Count(),
                Plist = list.Skip((dto.Pageindex - 1) * dto.Pagesize).Take(dto.Pagesize).ToList(),
            };
            return jou;
        }
        /// <summary>
        /// 添加系统日志
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public async Task<int> LogAdd(Journal m)
        {
            m.Operatorname = m.Operatorname.Trim();
            ilogRecord.Add(m);
            return await unOfWork.SaveChanges();
        }
        /// <summary>
        /// 获取日志，不分页
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<List<Journal>> LogList(PagelogRecordDTO dto)
        {
            var loge = await ilogRecord.GetAll();
            var list = from a in loge
                       where (string.IsNullOrEmpty(dto.Operatorname) || a.Operatorname.Contains(dto.Operatorname))
                       && a.Classify.Equals(dto.Classify)//根据分类名查询
                       orderby a.Createtimes descending
                       select a;
            return list.ToList();
        }
    }
}
